package com.open.capacity.reptile.task;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.open.capacity.finance.task.ScheduledOfTask;
import com.open.capacity.reptile.constant.GDSZCZConstant;
import com.open.capacity.reptile.dto.common.ResponseWithCookies;
import com.open.capacity.reptile.dto.gdszcz.request.ApiExplainRequest;
import com.open.capacity.reptile.dto.gdszcz.request.TokenRequest;
import com.open.capacity.reptile.dto.gdszcz.response.ApiExplainResponse;
import com.open.capacity.reptile.dto.gdszcz.response.CommonResponse;
import com.open.capacity.reptile.service.GdszczPayCardRecordService;
import com.open.capacity.reptile.service.GdszczPayPlanVoucherService;
import com.open.capacity.reptile.service.GdszczPayVoucherBillService;
import com.open.capacity.reptile.util.HttpUtils;
import com.open.capacity.user.service.SysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.apache.http.message.BasicHeader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.MessageFormat;
import java.util.List;


@Component
@Slf4j
public class GDSZCZDataTask implements ScheduledOfTask {

    @Autowired
    private SysConfigService configService;

    @Autowired
    private static GdszczPayCardRecordService payCardRecordService;

    @Autowired
    private static GdszczPayVoucherBillService payVoucherBillService;

    @Autowired
    private static GdszczPayPlanVoucherService payPlanVoucherService;

    Integer cronId = null;

    @Override
    public void setCronId(Integer cronId) {
        cronId = cronId;
    }

    @Override
    public void execute() throws UnsupportedEncodingException {

    }

    @Override
    public void trigger(String startTime) throws IOException {

    }


    //TODO 调试版，待删除
    public static void main(String[] args) throws IOException {

        //1. 获取token
        TokenRequest requestToken = new TokenRequest();
        requestToken.setAppId("gzcz");
        requestToken.setSecret("XqhGW3k79GOz94s");
        String responseToken = HttpUtils.requestForJson(MessageFormat.format(GDSZCZConstant.gettoken_url, "http://172.15.239.224:9003"), JSONObject.toJSONString(requestToken), new Header[0]);

        CommonResponse<String> responseTokenData = JSONObject.parseObject(responseToken, new TypeReference<CommonResponse<String>>() {
        });

        if (!responseTokenData.getResultCode().equals("000")) {

        }

        Header[] headers = new Header[1];
        headers[0] = new BasicHeader("X-Aa-Token", responseTokenData.getData());


        //2. api_explain   //TODO 不一定需要，如果不要可以删除
        ApiExplainRequest requestApiExplain = new ApiExplainRequest();
        requestApiExplain.setDtgCode("api_explain");
        String responseApiExplain = HttpUtils.requestForJson(MessageFormat.format(GDSZCZConstant.apiexplain_url, "http://172.15.239.224:9003"), JSONObject.toJSONString(requestApiExplain), headers);

        CommonResponse<List<ApiExplainResponse>> responseApiExplainData = JSONObject.parseObject(responseApiExplain, new TypeReference<CommonResponse<List<ApiExplainResponse>>>() {
        });

        if (!responseApiExplainData.getResultCode().equals("000")) {

        }

        //                //凭证主表(上报财政)
//                CommonRequest request = gdszczUtils.packetRequest(agencyCode, fiscalYear, updateStartTime, "gla_vou_head");

//                String responseGlaVouHead = HttpUtils.requestForJson(GDSZCZConstant.gla_vou_head_query_url, JSONObject.toJSONString(request), headers);
//
//                CommonResponse<List<GlaVouHeadData>> responseGlaVouHeadData = JSONObject.parseObject(responseGlaVouHead, new TypeReference<CommonResponse<List<GlaVouHeadData>>>() {
//                });
//                log.info(String.format("gla_vou_head_query_url--请求:agencyCode:{},fiscalYear:{},updateStartTime:{};响应:resultCode：{},resultMessage:{},resultCount:{}"), request.getAgencyCode(), request.getFiscalYear(), request.getUpdateStartTime(), responseGlaVouHeadData.getResultCode(), responseGlaVouHeadData.getResultMessage(), responseGlaVouHeadData.getData().size());
//                if (responseGlaVouHeadData.getResultCode().equals("000")) {
//                    for (GlaVouHeadData item : responseGlaVouHeadData.getData()) {
//                        GdszczGlaVouHead s = new GdszczGlaVouHead();
//                        BeanUtils.copyProperties(item, s);
//
//                        int existCount = glaVouHeadService.exists(s.getVouId());
//                        if (existCount == 0) {
//                            if (first.isPresent()) {
//                                s.setAgencyCode(first.get().getId());
//                            }
//                            glaVouHeadService.save(s);
//                        }
//                    }
//                }
//
//                //2. 凭证明细(上报财政)
//                request = gdszczUtils.packetRequest(agencyCode, fiscalYear, updateStartTime, "gla_vou_detail");
//                String responseGlaVouDetail = HttpUtils.requestForJson(GDSZCZConstant.gla_vou_detail_query_url, JSONObject.toJSONString(request), headers);
//
//                CommonResponse<List<GlaVouDetailData>> responseGlaVouDetailData = JSONObject.parseObject(responseGlaVouDetail, new TypeReference<CommonResponse<List<GlaVouDetailData>>>() {
//                });
//                log.info(String.format("gla_vou_detail--请求:agencyCode:{},fiscalYear:{},updateStartTime:{};响应:resultCode：{},resultMessage:{},resultCount:{}"), request.getAgencyCode(), request.getFiscalYear(), request.getUpdateStartTime(), responseGlaVouDetailData.getResultCode(), responseGlaVouDetailData.getResultMessage(), responseGlaVouDetailData.getData().size());
//                if (responseGlaVouDetailData.getResultCode().equals("000")) {
//                    for (GlaVouDetailData item : responseGlaVouDetailData.getData()) {
//                        GdszczGlaVouDetail s = new GdszczGlaVouDetail();
//                        BeanUtils.copyProperties(item, s);
//
//                        int existCount = glaVouDetailService.exists(s.getVouDetId());
//                        if (existCount == 0) {
//                            if (first.isPresent()) {
//                                s.setAgencyCode(first.get().getId());
//                            }
//                            glaVouDetailService.save(s);
//                        }
//                    }
//                }

//
//        // //3. 获取支付申请
////        PayCommonRequest requestApplyPay = new PayCommonRequest();
////        requestApplyPay.setDtgCode("pay_voucher");
////        requestApplyPay.setFiscalYear(2023);
////        requestApplyPay.setMofDivCode("440100000");
////        requestApplyPay.setUserName("gdsgzsjcy");
////        requestApplyPay.setUserPass("AUUubPb1kDDM");
////        requestApplyPay.setUserType("3");
////        requestApplyPay.setAgencyCode("181001");
////        //requestApplyPay.setUpdateStartTime(System.currentTimeMillis() / 1000);
////        requestApplyPay.setUpdateStartTime("");
////        ResponseWithCookies responseApplyPay = HttpUtils.requestForJson(GDSZCZConstant.pay_apply_query_url, JSONObject.toJSONString(requestApplyPay), null, headers);
////
////        PayVoucherResponse responseApplyPayData = JSONObject.parseObject(responseApplyPay.getContent(), new TypeReference<PayVoucherResponse>() {
////        });
////
////        if (!responseApplyPayData.getResultCode().equals("000")) {
////
////        }
////
////
//
////
////
////        //5. 用款额度查询
////        PayCommonRequest requestPayPlanBalance = new PayCommonRequest();
////        requestPayPlanBalance.setDtgCode("pay_plan_balance");
////        requestPayPlanBalance.setFiscalYear(2023);
////        requestPayPlanBalance.setMofDivCode("440100000");
////        requestPayPlanBalance.setUserName("gdsgzsjcy");
////        requestPayPlanBalance.setUserPass("AUUubPb1kDDM");
////        requestPayPlanBalance.setUserType("3");
////        requestPayPlanBalance.setAgencyCode("181001");
////        //requestVoucherPay.setUpdateStartTime(System.currentTimeMillis() / 1000);
////        requestPayPlanBalance.setUpdateStartTime("");
////        ResponseWithCookies responsePayPlanBalance = HttpUtils.requestForJson(GDSZCZConstant.pay_plan_balance_query_url, JSONObject.toJSONString(requestPayPlanBalance), null, headers);
////
////        CommonResponse<List<PayPlanBalanceData>> responsePayPlanBalanceData = JSONObject.parseObject(responsePayPlanBalance.getContent(), new TypeReference<CommonResponse<List<PayPlanBalanceData>>>() {
////        });
////
////        if (!responsePayPlanBalanceData.getResultCode().equals("000")) {
////
////        }
//
//
////        //6. 零余额到账通知书查询
////        PayCommonRequest requestPlAmtArriveNotice = new PayCommonRequest();
////        requestPlAmtArriveNotice.setDtgCode("pl_amt_arrive_notice");
////        requestPlAmtArriveNotice.setFiscalYear(2023);
////        requestPlAmtArriveNotice.setMofDivCode("440100000");
////        requestPlAmtArriveNotice.setUserName("gdsgzsjcy");
////        requestPlAmtArriveNotice.setUserPass("AUUubPb1kDDM");
////        requestPlAmtArriveNotice.setUserType("3");
////        requestPlAmtArriveNotice.setAgencyCode("181001");
////        //requestPlAmtArriveNotice.setUpdateStartTime(System.currentTimeMillis() / 1000);
////        requestPlAmtArriveNotice.setUpdateStartTime("");
////        ResponseWithCookies responsePlAmtArriveNotice = HttpUtils.requestForJson(GDSZCZConstant.pl_amt_arrive_notice_query_url, JSONObject.toJSONString(requestPlAmtArriveNotice), null, headers);
////
////        CommonResponse<List<PlAmtArriveNoticeData>> responsePlAmtArriveNoticeData = JSONObject.parseObject(responsePlAmtArriveNotice.getContent(), new TypeReference<CommonResponse<List<PlAmtArriveNoticeData>>>() {
////        });
////
////        if (!responsePlAmtArriveNoticeData.getResultCode().equals("000")) {
////
////        }
//
//
//        //7. 支付更正申请书查询
////        PayCommonRequest requestPayApplyCorrection = new PayCommonRequest();
////        requestPayApplyCorrection.setDtgCode("pay_apply_correction");
////        requestPayApplyCorrection.setFiscalYear(2023);
////        requestPayApplyCorrection.setMofDivCode("440100000");
////        requestPayApplyCorrection.setUserName("gdsgzsjcy");
////        requestPayApplyCorrection.setUserPass("AUUubPb1kDDM");
////        requestPayApplyCorrection.setUserType("3");
////        requestPayApplyCorrection.setAgencyCode("181001");
////        //requestPayApplyCorrection.setUpdateStartTime(System.currentTimeMillis() / 1000);
////        requestPayApplyCorrection.setUpdateStartTime("");
////        ResponseWithCookies responsePayApplyCorrection = HttpUtils.requestForJson(GDSZCZConstant.pay_apply_correction_query_url, JSONObject.toJSONString(requestPayApplyCorrection), null, headers);
////
////        CommonResponse<List<PayApplyCorrectionData>> responsePayApplyCorrectionData = JSONObject.parseObject(responsePayApplyCorrection.getContent(), new TypeReference<CommonResponse<List<PayApplyCorrectionData>>>() {
////        });
////
////        if (!responsePayApplyCorrectionData.getResultCode().equals("000")) {
////
////        }
//
    }
}

